<?php
/************************FoxPHP威客系统************************/
/*                                                            */
/*  FoxPHP.Com (C)2006-2016 FoxPHP Inc.                       */
/*  This is NOT a freeware, use is subject to license terms   */
/*  FoxPHP威客系统是商业软件,使用于商业用途请购买授权         */
/*  V3.0 2016.3                                               */
/*  foxphp@aliyun.com                                         */
/*  http://www.foxphp.com                                     */
/*                                                            */
/**************************************************************/

namespace Login\Controller;
use Think\Controller;

@require_once(FOXPHP_PATH.'/Apps/Login/Class/saetv2.ex.class.php');

class SinaController extends Controller {
	
	public function indexAction(){
		
		$o = new \SaeTOAuthV2(C('sinaappkey'),C('sinaappsecret'));

		$sinaurl = $o->getAuthorizeURL(C('S_URL').'/login/sina/callback');

		header("location:$sinaurl");

	}
	
	public function callbackAction(){
		$o = new \SaeTOAuthV2(C('sinaappkey'),C('sinaappsecret'));

		if(isset($_REQUEST['code'])) {
			$keys = array();
			$keys['code'] = $_REQUEST['code'];
			$keys['redirect_uri'] = C('S_URL').'/login/sina/callback';
			try {
				$token = $o->getAccessToken( 'code', $keys ) ;
			} catch (OAuthException $e) {
			}
		}

		if ($token) {
			$c = new \SaeTClientV2(C('sinaappkey'),C('sinaappsecret'),$token['access_token'] );
			$uid_get = $c->get_uid();
			$user = $this->ConvertArray($c->show_user_by_id( $uid_get['uid']));

			if(!empty($user['name'])){

				unset($_SESSION['SinaKeys']);
				$weibo=new \Member\Model\MemberModel();
				if($weibo->LoginWeiBo(array('type'=>1,'apikey'=>$token['access_token'],'apiname'=>$user['name'],'followers'=>$user['followers_count']))){
					if($this->uid){
						$this->error('绑定成功！','/',3);
					}else{
						$this->error('登录成功！','/',3);
					}
				}else{
					$this->error('认证失败！！！','/',3);
					return false;
				}

			}else{

				$this->error('认证失败！！','/',3);
				return false;
			} 

		}else{
			$this->error('认证失败！','/',3);
			return false;
		} 
	}
    
}